package zunge.twitterdel.timestat;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;

import zunge.tools.DbConn;
import zunge.twitterdel.bean.TweetTime;
import zunge.twitterdel.bean.TweetTimeDAO;
import zunge.twitterdel.bean.TypoTweetDAO;

public class TimeStat {

	static String farHost = "rincewind.isr.cs.cmu.edu";//"128.237.116.53";
	static String farDb = "deletedtweets";
	static String farUser = "bliu1";
	static DbConn farConn;

	static String localHost = "localhost";
	static String localDb = "tweetstat";
	static String localUser = "root";
	static DbConn localConn;
	
	static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.0");
	
	public static void main(String args[]){
		//storeTimeInfo();
		statOnTime();
	}
	
	static void storeTimeInfo(){
		farConn = new DbConn(farHost,farDb,farUser,null);
		
		System.out.println("Get Deleted TweetIDs");
		HashSet<Long> delTweets = new HashSet<Long>();
		String query =
			"SELECT tweet FROM deletedTweets";
		ResultSet rs = farConn.executeQuery(query);
		try{
			while(rs.next()){
				delTweets.add(rs.getLong("tweet"));
			}
			rs.close();
		}catch(Exception e){e.printStackTrace();}
		
		
		
		
		
		System.out.println("Get Time Info");
		List<TweetTime> res = new ArrayList<TweetTime>();
		
		query = 
			"SELECT max(userID) maxid FROM twitteruser";
		rs = farConn.executeQuery(query);
		long maxID = -10000000;
		try{
			rs.next();
			maxID = rs.getLong("maxid");
		}catch(Exception e){e.printStackTrace();}
		
		long step = 1000000;
		long cur = 0;
		while(cur < maxID+step){
			System.out.println("cur="+cur+"\tsize="+res.size());
			query =
				"SELECT " +
					" t.tweetID, t.user, t.createdAt, u.utcOffset " +
				"FROM " +
					"tweet t, twitteruser u " +
				"WHERE t.user = u.userID AND t.user > "+(cur-1)+" AND t.user < "+(cur+step);
			rs = farConn.executeQuery(query);
			try{
				while(rs.next()){
					long tweetID = rs.getLong("tweetID");
					long userID = rs.getLong("user");
					java.util.Date createdAt = sdf.parse(rs.getString("createdAt"));
					int userUtcOffset = rs.getInt("utcOffset");
					java.util.Date realTime = getRealTime(createdAt, userUtcOffset);
					
					TweetTime record = new TweetTime();
					record.setTweetID(tweetID);
					record.setUserID(userID);
					record.setRealTime(realTime);
					record.setUserUtcOffset(userUtcOffset);
					record.setDeleted(delTweets.contains(tweetID));
					res.add(record);
				}
			}catch(Exception e){e.printStackTrace();}
			cur += step;
		}
		farConn.close();
		
		System.out.println("Write to local");
		String jdbcURL = "jdbc:mysql://"+localHost+"/"+localDb;
		try{
			TweetTimeDAO tdao = new TweetTimeDAO(jdbcURL,localUser,null,"tweetTimeTemp");
			for(TweetTime record:res){
				tdao.create(record);
			}
		}catch(Exception e){e.printStackTrace();}
		
		System.out.println("Done");
	}
	static java.util.Date getRealTime(java.util.Date date, int utcoffset){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR, (utcoffset/3600));
		return calendar.getTime();
	}
	
	
	
	
	
	static void statOnTime(){
		localConn = new DbConn(localHost,localDb,localUser,null);
		String query =
			"SELECT realtime FROM tweettimetemp WHERE deleted = 1";
		ResultSet rs = localConn.executeQuery(query);
		List<java.util.Date> dates = new ArrayList<java.util.Date>();
		try{
			while(rs.next()){
				dates.add(sdf.parse(rs.getString("realtime")));
			}
		}catch(Exception e){e.printStackTrace();}
		double[] alldist = getDist(dates);
		for(int i = 0;i < 24;++i){System.out.println(alldist[i]);}
	}
	
	static double[] getDist(List<java.util.Date> dates){
		double[] res = new double[24];
		Arrays.fill(res, 0);
		
		for(java.util.Date date:dates){
			res[date.getHours()] += 1;
		}
		double sum = 0;
		for(int i = 0;i < 24;++i){
			sum += res[i];
		}
		for(int i = 0;i < 24;++i){
			res[i] = res[i]/sum;
		}
		return res;
	}
}